package com.youhome.controller;

import com.github.pagehelper.PageInfo;
import com.youhome.model.BClient;
import com.youhome.model.BCounselor;
import com.youhome.model.BShareGroup;
import com.youhome.response.Result;
import com.youhome.response.ResultCode;
import com.youhome.response.ResultPage;
import com.youhome.service.CounselorService;
import com.youhome.service.ShareGroupService;
import com.youhome.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * Created by chen365 on 2018/09/18.
 */
@Api(tags = "分享组")
@RestController
@RequestMapping(value = "/share_groups")
public class ShareGroupController {

    @Autowired
    private CounselorService counselorService;
    @Autowired
    private ShareGroupService shareGroupService;

    @ApiOperation(value = "列表", notes = "列表", response = BShareGroup.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "pageNumber", value = "当前页码", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页条数", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "status", value = "状态,0关闭,1开启", required = false, paramType = "query")
    })
    @GetMapping(value = "")
    public Object list(HttpServletRequest request, Integer pageNumber, Integer pageSize, Integer status) {

        BShareGroup record = new BShareGroup();
        BCounselor bCounselor = counselorService.selectByToken(request);
        if (bCounselor != null) {
            record.setCounselorId(bCounselor.getCounselorId());
        }
        if (!StringUtils.isEmpty(status)) {
            if (status.equals(0) || status.equals(1)) {
                record.setStatus(status);
            }
        }
        PageInfo<BShareGroup> bClientPageInfo = shareGroupService.selectByBean(pageNumber, pageSize, record);
        return new ResultPage<>(ResultCode.SUCCESS, bClientPageInfo);
    }

    @ApiOperation(value = "新增", notes = "新增", response = BClient.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "name", value = "分享组名称", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "status", value = "状态,0关闭,1开启", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "remark", value = "备注", required = false, paramType = "query")
    })
    @PostMapping(value = "")
    public Object add(HttpServletRequest request, String name, Integer status, String remark) {

        BCounselor bCounselor = counselorService.selectByToken(request);
        if (bCounselor == null) {
            return new Result<>(ResultCode.LAND_EXPIRATION);
        }

        BShareGroup bShareGroup = new BShareGroup();
        bShareGroup.setCounselorId(bCounselor.getCounselorId()); //顾问ID

        if (StringUtils.isEmpty(name)) {
            return new Result<>(ResultCode.EMPTY_NAME);
        }
        bShareGroup.setName(name);

        if (StringUtils.isEmpty(status)) {
            return new Result<>(ResultCode.EMPTY_STATUS);
        }

        if (!StringUtils.isEmpty(status)) {
            if (status.equals(0) || status.equals(1)) {
                bShareGroup.setStatus(status);
            } else {
                return new Result<>(ResultCode.ERROR_PARAMETER);
            }
        }

        if (!StringUtils.isEmpty(remark)) {
            bShareGroup.setRemark(remark);
        }
        String shareId = UUID.randomUUID().toString();
        bShareGroup.setShareId(shareId);
        int success = shareGroupService.insert(bShareGroup);
        if (success > 0) {
            return new Result<>(ResultCode.SUCCESS);
        }

        return new Result<>(ResultCode.FAIL);
    }


    @ApiOperation(value = "修改", notes = "修改", response = BClient.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "shareId", value = "分享组ID", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "name", value = "分享组名称", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "status", value = "状态,0关闭,1开启", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "remark", value = "备注", required = false, paramType = "query")
    })
    @PutMapping(value = "/{shareId}")
    public Object update(HttpServletRequest request,@PathVariable String shareId, String name, Integer status, String remark) {

        BCounselor bCounselor = counselorService.selectByToken(request);
        if (bCounselor == null) {
            return new Result<>(ResultCode.LAND_EXPIRATION);
        }

        BShareGroup bShareGroup = new BShareGroup();
        bShareGroup.setCounselorId(bCounselor.getCounselorId()); //顾问ID

        if (StringUtils.isEmpty(shareId)) {
            return new Result<>(ResultCode.ERROR_PARAMETER);
        }
        bShareGroup.setShareId(shareId);
        if (StringUtils.isEmpty(name)) {
            return new Result<>(ResultCode.EMPTY_NAME);
        }
        bShareGroup.setName(name);

        if (StringUtils.isEmpty(status)) {
            return new Result<>(ResultCode.EMPTY_STATUS);
        }
        if (!StringUtils.isEmpty(status)) {
            if (status.equals(0) || status.equals(1)) {
                bShareGroup.setStatus(status);
            } else {
                return new Result<>(ResultCode.ERROR_PARAMETER);
            }
        }

        if (!StringUtils.isEmpty(remark)) {
            bShareGroup.setRemark(remark);
        }

        int success = shareGroupService.updateByPrimaryKey(bShareGroup);
        if (success > 0) {
            return new Result<>(ResultCode.SUCCESS);
        }

        return new Result<>(ResultCode.FAIL);
    }


    @ApiOperation(value = "删除", notes = "删除", response = BCounselor.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "shareId", value = "分享组Id", required = true, paramType = "path")
    })
    @DeleteMapping(value = "/{shareId}")
    public Object delete(HttpServletRequest request,@PathVariable String shareId) {
        if (StringUtils.isEmpty(shareId)) {
            return new Result<>(ResultCode.EMPTY_PARAMETER);
        }
        BCounselor bCounselor = counselorService.selectByToken(request);
        if (bCounselor == null) {
            return new Result<>(ResultCode.LAND_EXPIRATION);
        }

        BShareGroup bShareGroup = new BShareGroup();
        bShareGroup.setShareId(shareId); //分享组Id
        bShareGroup.setCounselorId(bCounselor.getCounselorId()); //顾问ID

        if (shareGroupService.deleteByShareIdAndCounselorId(bShareGroup) > 0) {
            return new Result<>(ResultCode.SUCCESS);
        }

        return new Result<>(ResultCode.FAIL);
    }
}
